package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.DashboardDto;
import com.example.front.dto.DashboardSearchDto;
import com.example.front.dto.DashboardUpdataDto;
import com.example.front.entity.Dashboard;
import com.example.front.serivce.BoxDashboardService;
import com.example.front.serivce.DashboardService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Api(tags = "看板管理")
@CrossOrigin
@RestController
@RequestMapping("/front/dashboard")
public class DashboardController {

    @Resource
    DashboardService dashboardService;

    @Resource
    BoxDashboardService boxDashboardService;

    @ApiOperation("新增看板")
    @PostMapping("/create")
    public JsonResult<String> create(String name) {
        User user = UserContext.getUser();

        if (name == null) {
            return new JsonResult<>(false, "看板名称不能为空");
        }
        dashboardService.createDashboard(user.getTenantId(), name);

        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("获取看板列表")
    @GetMapping("/list")
    public JsonResult<ListDto<Dashboard>> list(DashboardSearchDto condition, Pagination pagination) {
        User user = UserContext.getUser();
        condition.setTenantId(user.getTenantId());
        return new JsonResult<>(dashboardService.listDashboard(condition, pagination));
    }

    @ApiOperation("修改看板(名称)")
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody  DashboardDto dashboardDto) {
        User user = UserContext.getUser();

        Dashboard dashboard = Dashboard.builder().
                id(dashboardDto.getId()).
                name(dashboardDto.getName()).
                tenantId(user.getTenantId()).build();

        dashboardService.updateDashboard(dashboard);
        DashboardUpdataDto dashboardUpdataDto = DashboardUpdataDto.builder().
                id(dashboardDto.getId()).
                name(dashboardDto.getName()).
                boxList(dashboardDto.getBoxList()).
                tenantId(user.getTenantId()).build();

        boxDashboardService.update(dashboardUpdataDto);

        return new JsonResult<>();
    }

    @ApiOperation("删除看板")
    @PostMapping("/delete")
    public JsonResult<String> delete(long id) {
        User user = UserContext.getUser();

        dashboardService.deleteDashboard(user.getTenantId(), id);

        return new JsonResult<>(true, "删除成功");
    }
}
